// 引入 extend和loaclize，定义规则和语言
import { extend, localize } from "vee-validate"

// 引入VeeValidate已有的规则
import { required, email, min, max, length, confirmed } from "vee-validate/dist/rules"

// 引入VeeValidate已有的中文环境报错提示
import zh from "vee-validate/dist/locale/zh_CN.json"
// import en from "vee-validate/dist/locale/en.json"

extend("email", email)
extend("min", min)
extend("max", max)
extend("required", required)
extend("length", length)
extend("confirmed", confirmed)

extend("password", {
    validate(value, { min, max }) {
        return value.length >= min && value.length <= max
    },
    params: ["min", "max"]
})

extend("judgeOdd", {
    validate(value) {
        return (value % 2) >= 1;
    },
    message: "{_field_}校验未通过，请输入正确的奇数"

})

localize({
    "zh_CN": {
        names: {
            email: "邮箱",
            password: "密码",
            judgeOdd: "校验奇数"
        },
        messages: {
            ...zh.messages,
            required: "请输入{_field_}",
            email: "请输入正确的邮箱格式",
            password: "密码必须是6到18位",
            judgeNum: "请输入奇数"
        }
    },
    // en: {
    //     names: {
    //         email: "email",
    //         password: "password"
    //     },
    //     messages: {
    //         ...en.messages,
    //         required: "please input {_field_}",
    //         email: "please input correct email",
    //         password: "the password length should be 6 to 18"
    //     }
    // }
})

// localize("zh-CN")

// 懒加载语言文件
function loadLocale(code) {
    return import(`vee-validate/dist/locale/${code}.json`).then(locale => {
        localize(code, locale);
    });
}

loadLocale("zh_CN")
